#include "main.h"
#include "ParseNIT.h"
#include "ParseTS_Head.h"
#include "ParseType.h"

void ParseNIT_Section(unsigned char NITtable[1200], int TransportPosition, TS_NIT *NewNitPoint);
static int i,n,l;
static unsigned int NIT_CRC[4096];
static unsigned char NIT_Long_Table[1200];
static unsigned char CountNit;

void TSNIT_Table(unsigned char NITtable[1200],TS_NIT *NewNitPoint)
{
	int NetworkDescriptorLen = 0;
	int NIT_Length = 0;
	int TransportPosition = 0;

	NewNitPoint->table_id = NITtable[0];
	NewNitPoint->section_syntax_indicator = NITtable[1] >> 7;
	NewNitPoint->reserved_future_use_1 = (NITtable[1] >> 6) & 0x01;
	NewNitPoint->reserved_1 = (NITtable[1] >> 4) & 0x03;
	NewNitPoint->section_length = ((NITtable[1] & 0x0F) << 8) | NITtable[2];
	NewNitPoint->network_id = (NITtable[3] << 8) | NITtable[4];
	NewNitPoint->reserved_2 = NITtable[5] >> 6;
	NewNitPoint->version_number = (NITtable[5] >> 1) & 0x1F;
	NewNitPoint->current_next_indicator = (NITtable[5] << 7) >> 7;
	NewNitPoint->section_number = NITtable[6];
	NewNitPoint->last_section_number = NITtable[7];
	NewNitPoint->reserved_future_use_2 = NITtable[8] >> 4;
	NewNitPoint->network_descriptors_length = ((NITtable[8] & 0x0F) << 8) | NITtable[9];
	NetworkDescriptorLen = NewNitPoint->network_descriptors_length;

	if(NewNitPoint->network_descriptors_length != 0)
	{
		memcpy(NewNitPoint->network_descriptor, NITtable + 10, NetworkDescriptorLen);
	}
	NewNitPoint->reserved_future_use_2 = NITtable[10 + NetworkDescriptorLen] >> 4;
	NewNitPoint->transport_stream_loop_length = ((NITtable[10 + NetworkDescriptorLen] & 0x0F) << 8)
	        | NITtable[10 + NetworkDescriptorLen + 1];

	if(NewNitPoint->transport_stream_loop_length != 0)
	{
		TransportPosition = 10 + NetworkDescriptorLen + 2;
		ParseNIT_Section(NITtable, TransportPosition, NewNitPoint);
	}

	NIT_Length = NewNitPoint->section_length + 3;

	NewNitPoint->CRC_32 = (NITtable[NIT_Length - 4] << 24) | (NITtable[NIT_Length - 3] << 16)
	        | (NITtable[NIT_Length - 2] << 8) | NITtable[NIT_Length - 1];

	if(NewNitPoint->section_length > 188)		// 判断该数据包是否将表的数据传送完整
	{
		l = NewNitPoint->section_length;
	}

	if((NewNitPoint->section_length < 188 && NewNitPoint->CRC_32 != 0) || ((l / 188) == n && NewNitPoint->CRC_32 != 0))  //过滤掉数据不完整的数组
	{
		if(i == 0)										//创建链表，防止重复建立相同的链表
		{
			NewNitPoint->next = NULL;
			NewNitPoint->Previous = NitPoint;
			NitPoint->next = NewNitPoint;
			NitPoint = NitPoint->next;

			NIT_CRC[i] = NewNitPoint->CRC_32;

			i ++;
		}else
		{
			for(int p = 0; p < i; p++)
			{
				if(NIT_CRC[p] == NewNitPoint->CRC_32)
				{
					return;
				}
			}
				NewNitPoint->next = NULL;
				NewNitPoint->Previous = NitPoint;
				NitPoint->next = NewNitPoint;
				NitPoint = NitPoint->next;

				NIT_CRC[i] = NewNitPoint->CRC_32;

				i ++;
		}
	}
}

void ParseNIT_Section(unsigned char NITtable[1200], int TransportPosition, TS_NIT *NewNitPoint)
{
	int NIT_Length = 0;
	int TranportCount = 0;
	int NetworkDescriptorLen = 0;
	int l = 0;

	NetworkDescriptorLen = NewNitPoint->network_descriptors_length;
	NIT_Length = NewNitPoint->section_length + 3;

	for (TransportPosition; TransportPosition < NIT_Length - 4; TransportPosition += 6)
	{
		NewNitPoint->astNIT_Transport[TranportCount].transport_stream_id = (NITtable[TransportPosition] << 8)
		        | NITtable[TransportPosition + 1];
		NewNitPoint->astNIT_Transport[TranportCount].original_network_id = (NITtable[TransportPosition + 2] << 8)
		        | NITtable[TransportPosition + 3];
		NewNitPoint->astNIT_Transport[TranportCount].reserved_future_use = NITtable[TransportPosition + 4] >> 4;
		NewNitPoint->astNIT_Transport[TranportCount].transport_descriptors_length = ((NITtable[TransportPosition + 4] & 0x0F) << 8)
		        | NITtable[TransportPosition + 5];

		if (0 != NewNitPoint->astNIT_Transport[TranportCount].transport_descriptors_length)
		{
			memcpy(NewNitPoint->astNIT_Transport[TranportCount].descriptor, NITtable + 6 + TransportPosition,
			        NewNitPoint->astNIT_Transport[TranportCount].transport_descriptors_length);
			TransportPosition += NewNitPoint->astNIT_Transport[TranportCount].transport_descriptors_length;
		}

		TranportCount++;
	}
}

void ParseTS_NIT_1(int TsPackage_len,TS_Package_Head TS_PackageNITHead)
{
    TS_NIT TS_PackageNIT = {0};
	unsigned char NITtable[204] = {0};
	TS_NIT *NewNitPoint = (TS_NIT *)malloc(sizeof(TS_NIT));
	
	CountNit = TS_PackageNITHead.continuity_counter;

    memcpy(NITtable, TSdata+5, TsPackage_len-5);

    TSNIT_Table(NITtable, NewNitPoint);

	n = 0;	
	memset(NIT_Long_Table, 0, 1200);
	memcpy(NIT_Long_Table, TSdata+5, TsPackage_len-5);

}

void ParseTS_NIT_0(int TsPackage_len,TS_Package_Head TS_PackageNITHead)
{
    TS_NIT TS_PackageNIT = {0};
	unsigned char NITtable[204] = {0};

    memcpy(NITtable, TSdata+4, TsPackage_len-4);

	if(n == 0 && CountNit+1 == TS_PackageNITHead.continuity_counter)
	{
		memcpy(NIT_Long_Table+183, NITtable, TsPackage_len-4);
		CountNit++;
		n++;
	}else if(CountNit+1 == TS_PackageNITHead.continuity_counter)
	{
		memcpy(NIT_Long_Table+(183+(184*n)), NITtable, TsPackage_len-4);
		CountNit++;
		n++;
	}

	if((l / 188) == n)
	{
		TS_NIT *NewNitPoint = (TS_NIT *)malloc(sizeof(TS_NIT));
		TSNIT_Table(NIT_Long_Table, NewNitPoint);
		n = 0;
		memset(NIT_Long_Table, 0, 1200);
	}
}